//
//  MyWorksViewController.m
//  WeiWeiHaircut
//
//  Created by 超群 on 15/10/15.
//  Copyright © 2015年 超群. All rights reserved.
//

#import "MyWorksViewController.h"
#import "MyWorksSpecialCell.h"
#import "MyWorksCommonCell.h"
#import "WorkModel.h"
#import "MJRefresh.h"

@interface MyWorksViewController () <UITableViewDataSource,UITableViewDelegate>
@property (nonatomic, assign) NSInteger page;
@property (nonatomic, strong) NSMutableArray *datas;
@property (nonatomic, strong) MyWorksCommonCell *selectLike;
@end

@implementation MyWorksViewController
-(NSMutableArray *)datas{
    if (_datas == nil) {
        _datas = [[NSMutableArray alloc] init];
    }
    return _datas;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    _page = 1;
    [self titleWithSting:@"设计师作品"];
    [self addBtnOnNavWithTitle:@"返回" andImageName:@"返回" andTarget:self andAction:@selector(backAction) andFrame:CGRectMake(0, 0, 45, 20) andDirection:left];
    
    [self buildTableViewUI];
    [self requestData];
    [self downloadData];
    
    self.tableView.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        _page = 1;
        
        [self downloadData];
        
    }];
    
    self.tableView.footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        
        [self downloadData];
    }];
}
-(void)requestData
{
    [self requestWithKeys:@[@"user_id"] andValues:@[@(22)] andUrlKey:@"index/myHairList" isCache:YES];
}

-(void)backAction
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)buildTableViewUI
{
    self.tableView.delegate         = self;
    self.tableView.dataSource       = self;
    self.tableView.separatorStyle   = UITableViewCellSeparatorStyleNone;
    self.tableView.backgroundColor  = [UIColor groupTableViewBackgroundColor];
    [self.view addSubview:self.tableView];
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([MyWorksSpecialCell class]) bundle:nil] forCellReuseIdentifier:NSStringFromClass([MyWorksSpecialCell class])];
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([MyWorksCommonCell class]) bundle:nil] forCellReuseIdentifier:NSStringFromClass([MyWorksCommonCell class])];
}

#pragma mark - tableheaderView
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 10;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView* headerTitleView     = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 10)];
    [headerTitleView setBackgroundColor:[UIColor whiteColor]];
    headerTitleView.backgroundColor     = [UIColor groupTableViewBackgroundColor];
    return headerTitleView;
}

#pragma mark - tableView delegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.datas.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    WorkModel *workModel1 = self.datas[indexPath.section];
    NSArray *array = workModel1.img_list;
        return ((array.count-1)/3 + 1) * (WIDTH - 20)/3.0 + 83;

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(nonnull NSIndexPath *)indexPath
{
    
}

#pragma mark - tableView datasource
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    MyWorksCommonCell * cell  = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([MyWorksCommonCell class]) forIndexPath:indexPath];
    cell.controller = self;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.model = self.datas[indexPath.section];
    WorkModel *tmpModel = self.datas[indexPath.section];;
    cell.myBlock = ^(UIButton *btn){
        NSString *url = [NSString stringWithFormat:@"%@%@",newPublicURL,@"Hair.GetHairPeople"];
        self.selectLike = cell;
        [self.request QZRequest_POST:url parameters:@{@"user_id":[QZUserInfo userId],@"hair_id":tmpModel.id} tagNSString:@"喜欢" stopRequest:NO isSerializer:NO isCache:NO];
    };
    return cell;
}
-(void)likeClick:(UIButton *)btn{

    MyWorksCommonCell * cell = (MyWorksCommonCell *)[btn superview];
    
    NSString *url = [NSString stringWithFormat:@"%@%@",newPublicURL,@"Hair.GetHairPeople"];
    
    [self.request QZRequest_POST:url parameters:@{@"user_id":[QZUserInfo userId],@"hair_id":cell.model.id} tagNSString:@"喜欢" stopRequest:NO isSerializer:NO isCache:NO];
}
- (void)downloadData{
    NSString *url = [NSString stringWithFormat:@"%@%@",newPublicURL,@"Hair.GetHairDesign"];

    
    NSDictionary *dic = @{
                          @"user_id":self.designer_ID,
                          @"customer_id":[QZUserInfo userId],
                          @"page":@(_page),
                          @"sort":@"time",
                          @"sort_type":@"right",
                          @"limit":@(10)
                          };
    [self.request QZRequest_POST:url parameters:dic tagNSString:@"设计师作品" stopRequest:NO isSerializer:NO isCache:YES];

}

-(void)request_POST_FinishValue:(id)value tagNSString:(NSString *)tag{
    
    if ([tag isEqualToString:@"设计师作品"]) {
        
        
        if ([value[@"data"][@"status"] integerValue] == 100) {
            if (_page == 1) {
                
                [self.datas removeAllObjects];
            }
            _page ++ ;
            NSArray *array = value[@"data"][@"data"][@"list"];
            for (NSDictionary *dic in array) {
                WorkModel *work = [[WorkModel alloc] initWithDictionary:dic error:nil];
                [self.datas addObject:work];
            }
            
            [self.tableView reloadData];
        }else{
            [[iToast makeText:value[@"data"][@"info"]] show];
            return;
        }
        [self.tableView.header endRefreshing];
        [self.tableView.footer endRefreshing];
    
    }
    
    if ([tag isEqualToString:@"喜欢"]) {
        if ([value[@"data"][@"status"] integerValue] == 100) {
            NSInteger index = [value[@"data"][@"data"][@"is_people"] integerValue];
            self.selectLike.model.is_people = index;
            if (index == 1) {
                [self.selectLike.supportBtn setImage:[UIImage imageNamed:@"喜欢-图标-选中"] forState:UIControlStateNormal];
                self.selectLike.model.people = self.selectLike.model.people + 1;
                

            }else{
                [self.selectLike.supportBtn setImage:[UIImage imageNamed:@"形状-2"] forState:UIControlStateNormal];
                self.selectLike.model.people = self.selectLike.model.people - 1;
            }
            self.selectLike.surpportNumLabel.text = [NSString stringWithFormat:@"%ld", self.selectLike.model.people];
        }
    }
}
@end
